"""
Case Type   : synchronous_commit
Case Name   : gs_guc reload方式设置参数synchronous_commit为off，设置成功
Create At   : 2023/8/16
Owner       : li-xin12345
Description :
    1、查询synchronous_commit默认值
    2、修改synchronous_commit为'off', 'false', 'no', 0, 'FALSE', 'OFF'
    3、恢复参数
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Pri_SH = CommonSH('PrimaryDbUser')
Node_Num = Pri_SH.get_node_num()


@unittest.skipIf(3 > Node_Num, '不满足一主两备环境跳过')
class SynchronousCommitTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.tb_name = 't_china_mobile_0004'
        self.set_params = ['off', 'false', 'no', 0, 'FALSE', 'OFF']

    def test_synchronous_commit(self):
        text = '-----step1:查询synchronous_commit默认值;expect:成功-----'
        self.log.info(text)
        self.default_param = self.common.show_param('synchronous_commit')
        self.log.info(self.default_param)
        self.assertIn('on', self.default_param, '执行失败：' + text)

        text = '-----step2:修改synchronous_commit为off,false,no,0,FALSE,OFF;' \
               'expect:成功-----'
        self.log.info(text)
        for i, item in enumerate(self.set_params):
            res = Pri_SH.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'synchronous_commit={item}')
            self.log.info(res)
            self.assertTrue(res)
            res = Pri_SH.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                'synchronous_commit=on')
            self.log.info(res)
            self.assertTrue(res)

    def tearDown(self):
        self.log.info('-----step3:恢复环境;expect:成功-----')
        curr_param = self.common.show_param('synchronous_commit')
        if curr_param != self.default_param:
            res = Pri_SH.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'synchronous_commit={self.default_param}')
            self.log.info(res)
            self.assertTrue(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')